//
//  CellStyleable.swift
//  RxDemo
//
//  Created by MAC on 2019/6/3.
//  Copyright © 2019 zk. All rights reserved.
//

import UIKit
import SnapKit
import Then

fileprivate struct Metric {
    
    static let margin: CGFloat = 10.0
    static let lineHeight: CGFloat = 1.0/UIScreen.main.scale
}


public enum CellLineStyle: Int {
    
    case `default`
    case none
    case full
    case margin
    case marginLeft
    case marginRight
}


protocol CellStyleable {
    
}


//MARK: - UITableViewCell
extension CellStyleable where Self: UITableViewCell {
    
    //MARK: 底部横线
    func bottomLine(style: CellLineStyle) -> UIView {
        
        let bottomLine = UIView().then {
            $0.backgroundColor = UIColor.gray
        }
        
        contentView.addSubview(bottomLine)
        
        bottomLine.snp.makeConstraints { (make) in
            make.height.equalTo(Metric.lineHeight)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        switch style {
        case .none:
            bottomLine.isHidden = true
        case .full:
            bottomLine.isHidden = false
            bottomLine.snp.updateConstraints { (make) in
                make.left.equalToSuperview()
            }
        case .margin:
            bottomLine.isHidden = false
            bottomLine.snp.updateConstraints { (make) in
                make.left.equalTo(Metric.margin * 2)
                make.right.equalTo( -Metric.margin * 2)
            }
        case .marginLeft:
            bottomLine.isHidden = false
            bottomLine.snp.updateConstraints { (make) in
                make.left.equalTo(Metric.margin * 2)
            }
        case .marginRight:
            bottomLine.isHidden = false
            bottomLine.snp.updateConstraints { (make) in
                make.right.equalTo(-Metric.margin * 2)
            }
        default:
            break
        }
        return bottomLine
    }
}


//MARK: - UICollectionViewCell
extension CellStyleable where Self: UICollectionViewCell {
    
    //MARK: 顶部横线
    func topLine(style: CellLineStyle) -> UIView {
        
        let topLine = UIView()
        topLine.backgroundColor = UIColor.gray
        contentView.addSubview(topLine)
        
        topLine.snp.makeConstraints { (make) in
            make.height.equalTo(Metric.lineHeight)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        switch style {
        case .none:
            topLine.isHidden = true
        case .full:
            topLine.isHidden = false
            topLine.snp.updateConstraints { (make) in
                make.left.equalToSuperview()
            }
        case .margin:
            topLine.isHidden = false
            topLine.snp.updateConstraints { (make) in
                make.left.equalTo(Metric.margin * 2)
                make.right.equalTo( -Metric.margin * 2)
            }
        case .marginLeft:
            topLine.isHidden = false
            topLine.snp.updateConstraints { (make) in
                make.left.equalTo(Metric.margin * 2)
            }
        case .marginRight:
            topLine.isHidden = false
            topLine.snp.updateConstraints { (make) in
                make.right.equalTo(-Metric.margin * 2)
            }
        default:
            break
        }
        
        return topLine
    }
    
    //MARK: 底部横线
    func bottomLine(style: CellLineStyle) -> UIView {
        
        let bottomLine = UIView()
        bottomLine.backgroundColor = UIColor.gray
        
        contentView.addSubview(bottomLine)
        
        bottomLine.snp.makeConstraints { (make) in
            make.height.equalTo(Metric.lineHeight)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        switch style {
        case .none:
            bottomLine.isHidden = true
        case .full:
            bottomLine.isHidden = false
            bottomLine.snp.updateConstraints { (make) in
                make.left.equalToSuperview()
            }
        case .margin:
            bottomLine.isHidden = false
            bottomLine.snp.updateConstraints { (make) in
                make.left.equalTo(Metric.margin * 2)
                make.right.equalTo( -Metric.margin * 2)
            }
        case .marginLeft:
            bottomLine.isHidden = false
            bottomLine.snp.updateConstraints { (make) in
                make.left.equalTo(Metric.margin * 2)
            }
        case .marginRight:
            bottomLine.isHidden = false
            bottomLine.snp.updateConstraints { (make) in
                make.right.equalTo(-Metric.margin * 2)
            }
        default:
            break
        }
        return bottomLine
    }
}


